home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP08 / HEAD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  6.9 KB  |  183 lines

  1. /*---------------------------------------------
  2.    HEAD.C -- Displays beginning (head) of file
  3.              (c) Charles Petzold, 1996
  4.   ---------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <string.h>
  8. #include <direct.h>
  9.  
  10. #define  MAXPATH     256
  11. #define  MAXREAD    8192
  12.  
  13. LRESULT CALLBACK WndProc  (HWND, UINT, WPARAM, LPARAM) ;
  14. LRESULT CALLBACK ListProc (HWND, UINT, WPARAM, LPARAM) ;
  15.  
  16. WNDPROC fnOldList ;
  17.  
  18. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  19.                     PSTR szCmdLine, int iCmdShow)
  20.      {
  21.      static char szAppName[] = "Head" ;
  22.      HWND        hwnd ;
  23.      MSG         msg ;
  24.      WNDCLASSEX  wndclass ;
  25.  
  26.      wndclass.cbSize        = sizeof (wndclass) ;
  27.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  28.      wndclass.lpfnWndProc   = WndProc ;
  29.      wndclass.cbClsExtra    = 0 ;
  30.      wndclass.cbWndExtra    = 0 ;
  31.      wndclass.hInstance     = hInstance ;
  32.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  33.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  34.      wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1) ;
  35.      wndclass.lpszMenuName  = NULL ;
  36.      wndclass.lpszClassName = szAppName ;
  37.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  38.  
  39.      RegisterClassEx (&wndclass) ;
  40.  
  41.      hwnd = CreateWindow (szAppName, "File Head",
  42.                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  43.                           CW_USEDEFAULT, CW_USEDEFAULT,
  44.                           CW_USEDEFAULT, CW_USEDEFAULT,
  45.                           NULL, NULL, hInstance, NULL) ;
  46.  
  47.      ShowWindow (hwnd, iCmdShow) ;
  48.      UpdateWindow (hwnd) ;
  49.  
  50.      while (GetMessage (&msg, NULL, 0, 0))
  51.           {
  52.           TranslateMessage (&msg) ;
  53.           DispatchMessage (&msg) ;
  54.           }
  55.      return msg.wParam ;
  56.      }
  57.  
  58. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  59.      {
  60.      static BOOL     bValidFile ;
  61.      static char     sReadBuffer[MAXREAD], szFile[MAXPATH] ;
  62.      static HWND     hwndList, hwndText ;
  63.      static OFSTRUCT ofs ;
  64.      static RECT     rect ;
  65.      char            szBuffer[MAXPATH + 1] ;
  66.      HDC             hdc ;
  67.      int             iHandle, i ;
  68.      PAINTSTRUCT     ps ;
  69.      TEXTMETRIC      tm ;
  70.  
  71.      switch (iMsg)
  72.           {
  73.           case WM_CREATE :
  74.                hdc = GetDC (hwnd) ;
  75.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  76.                GetTextMetrics (hdc, &tm) ;
  77.                ReleaseDC (hwnd, hdc) ;
  78.  
  79.                rect.left = 20 * tm.tmAveCharWidth ;
  80.                rect.top  =  3 * tm.tmHeight ;
  81.  
  82.                hwndList = CreateWindow ("listbox", NULL,
  83.                               WS_CHILDWINDOW | WS_VISIBLE | LBS_STANDARD,
  84.                               tm.tmAveCharWidth, tm.tmHeight * 3,
  85.                               tm.tmAveCharWidth * 13 +
  86.                                    GetSystemMetrics (SM_CXVSCROLL),
  87.                               tm.tmHeight * 10,
  88.                               hwnd, (HMENU) 1,
  89.                               (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  90.                               NULL) ;
  91.  
  92.                hwndText = CreateWindow ("static", getcwd (szBuffer, MAXPATH),
  93.                               WS_CHILDWINDOW | WS_VISIBLE | SS_LEFT,
  94.                               tm.tmAveCharWidth,           tm.tmHeight,
  95.                               tm.tmAveCharWidth * MAXPATH, tm.tmHeight,
  96.                               hwnd, (HMENU) 2,
  97.                               (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  98.                               NULL) ;
  99.  
  100.                fnOldList = (WNDPROC) SetWindowLong (hwndList, GWL_WNDPROC,
  101.                                                     (LPARAM) ListProc) ;
  102.  
  103.                SendMessage (hwndList, LB_DIR, 0x37, (LPARAM) "*.*") ;
  104.                return 0 ;
  105.  
  106.           case WM_SIZE :
  107.                rect.right  = LOWORD (lParam) ;
  108.                rect.bottom = HIWORD (lParam) ;
  109.                return 0 ;
  110.  
  111.           case WM_SETFOCUS :
  112.                SetFocus (hwndList) ;
  113.                return 0 ;
  114.  
  115.           case WM_COMMAND :
  116.                if (LOWORD (wParam) == 1 && HIWORD (wParam) == LBN_DBLCLK)
  117.                     {
  118.                     if (LB_ERR == (i = SendMessage (hwndList,
  119.                                                     LB_GETCURSEL, 0, 0L)))
  120.                          break ;
  121.  
  122.                     SendMessage (hwndList, LB_GETTEXT, i, (LPARAM) szBuffer) ;
  123.  
  124.                     if (-1 != OpenFile (szBuffer, &ofs, OF_EXIST | OF_READ))
  125.                          {
  126.                          bValidFile = TRUE ;
  127.                          strcpy (szFile, szBuffer) ;
  128.                          getcwd (szBuffer, MAXPATH) ;
  129.                          if (szBuffer [strlen (szBuffer) - 1] != '\\')
  130.                               strcat (szBuffer, "\\") ;
  131.                          SetWindowText (hwndText, strcat (szBuffer, szFile)) ;
  132.                          }
  133.                     else
  134.                          {
  135.                          bValidFile = FALSE ;
  136.                          szBuffer [strlen (szBuffer) - 1] = '\0' ;
  137.                          chdir (szBuffer + 1) ;
  138.                          getcwd (szBuffer, MAXPATH) ;
  139.                          SetWindowText (hwndText, szBuffer) ;
  140.                          SendMessage (hwndList, LB_RESETCONTENT, 0, 0L) ;
  141.                          SendMessage (hwndList, LB_DIR, 0x37, (LONG) "*.*") ;
  142.                          }
  143.                     InvalidateRect (hwnd, NULL, TRUE) ;
  144.                     }
  145.                return 0 ;
  146.  
  147.           case WM_PAINT :
  148.                hdc = BeginPaint (hwnd, &ps) ;
  149.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  150.                SetTextColor (hdc, GetSysColor (COLOR_BTNTEXT)) ;
  151.                SetBkColor   (hdc, GetSysColor (COLOR_BTNFACE)) ;
  152.  
  153.                if (bValidFile && -1 != (iHandle =
  154.                          OpenFile (szFile, &ofs, OF_REOPEN | OF_READ)))
  155.                     {
  156.                     i = _lread (iHandle, sReadBuffer, MAXREAD) ;
  157.                     _lclose (iHandle) ;
  158.                     DrawText (hdc, sReadBuffer, i, &rect, DT_WORDBREAK |
  159.                                    DT_EXPANDTABS | DT_NOCLIP | DT_NOPREFIX) ;
  160.                     }
  161.                else
  162.                     bValidFile = FALSE ;
  163.  
  164.                EndPaint (hwnd, &ps) ;
  165.                return 0 ;
  166.  
  167.           case WM_DESTROY :
  168.                PostQuitMessage (0) ;
  169.                return 0 ;
  170.           }
  171.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  172.      }
  173.  
  174. LRESULT CALLBACK ListProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  175.      {
  176.      if (iMsg == WM_KEYDOWN && wParam == VK_RETURN)
  177.  
  178.           SendMessage (GetParent (hwnd), WM_COMMAND, 1,
  179.                        MAKELONG (hwnd, LBN_DBLCLK)) ;
  180.  
  181.      return CallWindowProc (fnOldList, hwnd, iMsg, wParam, lParam) ;
  182.      }
  183.